public class SimpleNotepad implements INotepad {

	private Page[] pages;
	private int freePages;

	private SimpleNotepad() {
	}

	public SimpleNotepad(int size) {
		setPagesSize(size);
		pages[0] = new Page("Default");
		pages[0].setText("");
		freePages = pages.length - 1;

	}

	public Page[] getPages() {
		return pages;
	}

	private void setPagesSize(int size) {
		if (size >= 1) {
			pages = new Page[size];
		} else
			pages = new Page[1];
	}

	@Override
	public void addTextToPage(String text, int page) {
		if (page >= 0 && page < pages.length && text != null) {
			if (pages[page] == null) {
				pages[page] = new Page("Default " + page);
				pages[page].addText(text);
			} else
				pages[page].addText(text);
		}

	}

	@Override
	public void addNewTextToPage(String text, int page) {
		if (page >= 0 && page < pages.length) {
			if (pages[page] != null && text != null) {
				pages[page].removeText();
				addTextToPage(text, page);
			}else addTextToPage(text, page);
		}

	}

	@Override
	public void deleteTextFromPage(int page) {
		if (page >= 0 && page < pages.length && pages[page] != null)
			pages[page].setText("");
	}

	@Override
	public void printAllPages() {
		for (int i = 0; i < pages.length; i++) {
			if (pages[i] != null) {
				pages[i].viewPage();
			}
		}

	}

	@Override
	public void searchWord(String word) {
		boolean anyWhere = false;
		for (int i = 0; i < pages.length; i++) {
			if (pages[i]!=null && pages[i].searchWord(word)) {
				System.out.println(word + " is on page " + i);
				anyWhere = true;
			}
		}

		if (!anyWhere)
			System.out.println("Sorry");

	}

	@Override
	public void printAllPagesWithDigits() {
		for (int i = 0; i < pages.length; i++) {
			if (pages[i]!=null && pages[i].containsDigits()) {
				pages[i].viewPage();
			}

		}
	}

}
